﻿using MeterialManager.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MeterialManager
{
    public delegate void DelBindDgv();
    public partial class FrmCategoryManager : Form
    {
        DelBindDgv delBindDgv;
        public FrmCategoryManager()
        {
            InitializeComponent();
        }

        private void FrmCategoryManager_Load(object sender, EventArgs e)
        {
            BindDgv();
            delBindDgv = BindDgv;
        }

        private void BindDgv()
        {
            dgvCategoryManager.AutoGenerateColumns = false;
            List<Category> categories = Category.GetListCategory();
            dgvCategoryManager.DataSource = categories;
        }

        private void dgvCategoryManager_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                tsmAdd.Visible = true;
                tsmEdit.Visible = false;
                tsmDel.Visible = false;
            }
        }

        private void dgvCategoryManager_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (e.RowIndex > -1)
                {
                    dgvCategoryManager.Rows[e.RowIndex].Selected = true;
                    tsmAdd.Visible = false;
                    tsmEdit.Visible = true;
                    tsmDel.Visible = true;
                }
            }
        }

        private void tsmAdd_Click(object sender, EventArgs e)
        {
            FrmSetCategory frmSetCategory = new FrmSetCategory(delBindDgv);
            frmSetCategory.ShowDialog();
        }

        private void tsmEdit_Click(object sender, EventArgs e)
        {
            int categoryId = (int)dgvCategoryManager.SelectedRows[0].Cells["categoryId"].Value;
            FrmSetCategory frmSetCategory = new FrmSetCategory(delBindDgv, categoryId);
            frmSetCategory.ShowDialog();
        }

        private void tsmDel_Click(object sender, EventArgs e)
        {
            int categoryId = (int)dgvCategoryManager.SelectedRows[0].Cells["categoryId"].Value;
            if (MessageBox.Show("确认删除？", "是否删除？", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) == DialogResult.OK)
            {
                Category.Delete(categoryId);
            };
            BindDgv();
        }
    }
}
